package gr.atc.epart;

import gr.atc.epart.actions.ActionComment;
import gr.atc.epart.actions.Petition;
import gr.atc.epart.actions.PetitionSignature;
import gr.atc.epart.actions.Action.Type;
import gr.atc.epart.net.ActionsPage;
import gr.atc.epart.net.CreateFailedException;
import gr.atc.epart.net.IClient;
import gr.atc.epart.net.LogInException;
import gr.atc.epart.net.RetrievalFailedException;
import gr.atc.epart.net.ActionsPage.SortBy;

import java.util.Date;
import java.util.List;

import android.util.Log;
import android.util.Pair;

public class EPartClient implements IClient
{

	//
	// this provides all the implemented services
	//
	private EParticipationServicesProvider _services = EParticipationServicesProvider.getInstance();
	
	@Override
	public int createPetition(Petition petition) throws CreateFailedException
	{
		return _services.createPetition(petition);
	}

	@Override
	public ActionsPage getActions(int pageSession, int page)
	{
		
		return null;
	}
	
	@Override
	public ActionsPage getActions(Type actionType, String searchText,String creator,
			Date startDate, Date stopDate, String[] tags, SortBy sortBy,int pageNumber,int pageSize)
	{
		try
		{
			return _services.getActions(actionType, searchText, creator, startDate, stopDate, tags, sortBy, pageNumber, pageSize);
		} catch (RetrievalFailedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Petition getPetition(int petitionId) throws RetrievalFailedException
	{
	    return _services.getPetition(petitionId,null).first;
	}

	@Override
	public Pair<Petition,Boolean> getPetitionStatus(int petitionId,String userName) throws RetrievalFailedException
	{
		return _services.getPetition(petitionId,userName);
	}
	
	@Override
	public List<PetitionSignature> getSignaturesForPetition(int petitionId) throws RetrievalFailedException
	{
	    return _services.getSignaturesForPetition(petitionId);
	}
	
	@Override
	public void voteForPetition(int petitionId, String userName,String comment) throws CreateFailedException
	{
	    _services.voteForPetition(petitionId, userName,comment);
	}
	
	@Override
	public void logIn(String username, String password) throws LogInException
	{
		if (! _services.logInToServices(username, password))
		{
			Log.d("logIn","Failed");
		}
		else
		{
			Log.d("logIn","Success");
		}
		
	}

	@Override
	public void logOut()
	{

	}

	@Override
	public boolean createAction(Type actionType, String name, String description, String creator,Date startDate, Date stopDate, String[] tags,boolean isLoginRequired, int seriesId)
	{
		try
		{
			return _services.createAction(actionType, name, description, creator,startDate, stopDate, tags,isLoginRequired,seriesId);
		} catch (CreateFailedException e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public boolean commentOnAction(int actionId, String userName,String comment)
	{
		try
		{
			return _services.commentOnAction(actionId, userName, comment);
			
		} catch (CreateFailedException e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public List<ActionComment> getComments(int actionId, int pageNumber, int pageSize) throws RetrievalFailedException
	{
		return _services.getComments(actionId, pageNumber, pageSize);
	}

}
